package com.icebergteam.warfacewidget.core;

import com.icebergteam.warfacewidget.WidgetProvider;
import com.icebergteam.warfacewidget.data.sql.CacheTable;
import com.icebergteam.warfacewidget.data.sql.ClanTable;
import com.icebergteam.warfacewidget.data.sql.NewsTable;
import com.icebergteam.warfacewidget.data.sql.RankTable;
import com.icebergteam.warfacewidget.data.sql.UserRankTable;
import com.icebergteam.warfacewidget.data.sql.VideosTable;
import com.icebergteam.warfacewidget.data.sql.WikiTable;

import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public class Database extends SQLiteOpenHelper {
	public final static String	DB_NAME = WidgetProvider.ROOT_PATH + "/database/db";
	private final static int DB_VERSION = 4;
	public static String className = Database.class.getCanonicalName();
	
	// Tables
	private ClanTable clanTable;
	private NewsTable newsTable;
	private CacheTable cacheTable;
	private RankTable rankTable;
	private VideosTable videosTable;
	private WikiTable wikiTable;
	private UserRankTable userRankTable;
	
	public Database(Context context) {
		super(context, DB_NAME, null, DB_VERSION);
		LogManager.LogV(className, LogManager.getExecuteMethodName(),context);
		
		clanTable			= new ClanTable();
		newsTable 			= new NewsTable();
		cacheTable          = new CacheTable();
		rankTable			= new RankTable();
		videosTable			= new VideosTable();
		wikiTable			= new WikiTable();
		userRankTable 		= new UserRankTable();
	}

	@Override
	public void onCreate(SQLiteDatabase db) {
		LogManager.LogV(className, LogManager.getExecuteMethodName());
	
		db.execSQL(clanTable.createTableStr());
		db.execSQL(getCacheTable().createTableStr());
		db.execSQL( getNewsTable().createTableStr() );
		db.execSQL( getRankTable().createTableStr() );
		db.execSQL( getVideosTable().createTableStr() );
		db.execSQL(getWikiTable().createTableStr());
		db.execSQL(getUserRankTable().createTableStr());
	}
	
	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
		// Do Nothing	
		LogManager.LogV(className,LogManager.getExecuteMethodName(), db, oldVersion, newVersion);
	//	db.execSQL("DROP TABLE IF EXISTS news, clan " );
	//	onCreate(db);
		if(newVersion == 2){
			db.execSQL( getVideosTable().createTableStr());
		}
		else if(newVersion == 3){
			db.execSQL(getWikiTable().createTableStr());
		}
		else if(newVersion == 4){
			db.execSQL(getUserRankTable().createTableStr());
		}
	
	}
	
	public void createTable(String tableName, String...fields) {
		execSQL(createTableStr(tableName, fields));
	
	}
	public String createTableStr(String tableName, String...fields) {
		String query = "create table if not exists " + tableName + " (";
		for (int i = 0; i < fields.length; i++) {
			query += fields[i];
			if(i != fields.length - 1)
				query += ",";
			else
				query += ");";
		}
		return query;
	}
	
	public String wrapQuotes(String column){
		return "'" + column + "'";
	}
	
	public void execSQL(String sql) throws SQLException{
		this.getWritableDatabase().execSQL(sql);
	
	}

	public NewsTable getNewsTable() {
		return newsTable;
	}

	public Cursor query(String table, String [] columns, String selection,String [] selectionArgs, String groupBy, String  having, String orderBy, String limit) throws SQLException{
		SQLiteDatabase db = SQLiteDatabase.openOrCreateDatabase(Database.DB_NAME, null);
		return db.query(table, columns, selection, selectionArgs, groupBy, having, orderBy, limit);
	}

	/**
	 * @return the cacheTable
	 */
	public CacheTable getCacheTable() {
		return cacheTable;
	}
	
	public RankTable getRankTable() {
		return rankTable;
	}

	/**
	 * @return the videosTable
	 */
	public VideosTable getVideosTable() {
		return videosTable;
	}

	public WikiTable getWikiTable() {
		return wikiTable;
	}

	public UserRankTable getUserRankTable() {
		return userRankTable;
	}
}